Back to course home
0% completed
Vote For New Content
Microsoft Hard Problems
Regular Expression Matching (hard)
Solution: Regular Expression Matching
First Missing Positive (hard)
Solution: First Missing Positive
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Sliding Window Maximum (hard)
Solution: Sliding Window Maximum
Count Unique Characters of All Substrings of a Given String (hard)
Solution: Count Unique Characters of All Substrings of a Given String
Word Ladder (hard)
Solution: Word Ladder
Word Ladder II (hard)
Solution: Word Ladder II
Number of Flowers in Full Bloom (hard)
Solution: Number of Flowers in Full Bloom
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Matchsticks to Square (medium)
Problem Statement
You are given an integer array matchsticks, where matchsticks[i] represents the length of the ith matchstick. You need to make one square by using these all matchsticks. You are not allowed to break any matchstick, but you can link them together, and you can use each matchstick only once.
Return true, if you make one square using all given matchsticks. Otherwise, return false.
Examples
-
Example 1:
- Input:
[5,5,5,5] - Expected Output:
true - Justification: The matchsticks can be arranged to form a square with each side being 5 units long, using all the matchsticks exactly once.
- Input:
-
Example 2:
- Input:
[3,3,3,3,4] - Expected Output:
false - Justification: Despite having four matchsticks of the same length, the additional matchstick of length 4 prevents forming a perfect square.
- Input:
-
Example 3:
- Input:
[1,1,2,1,2,1] - Expected Output:
true - Justification: These matchsticks can be arranged to form a square with each side being 2 units long (
1+1for two sides and2for the other two sides).
- Input:
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Longest Arithmetic Subsequence
Solution: Matchsticks to Square
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself